package org.jivesoftware.spark.crowd.ui;

import java.awt.Color;
import java.util.List;

import javax.swing.Icon;
import javax.swing.JSplitPane;
import javax.swing.border.EmptyBorder;

import org.jivesoftware.resource.Res;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Message.Type;
import org.jivesoftware.smackx.MessageEventManager;
import org.jivesoftware.spark.ChatManager;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.crowd.CrowdRecorder;
import org.jivesoftware.spark.crowd.CrowdsManager;
import org.jivesoftware.spark.crowd.Param;
import org.jivesoftware.spark.crowd.impl.Crowd;
import org.jivesoftware.spark.crowd.impl.Member;
import org.jivesoftware.spark.crowd.packet.CrowdsMessageManager;
import org.jivesoftware.spark.ui.ChatRoom;
import org.jivesoftware.spark.ui.rooms.ChatRoomImpl;
import org.jivesoftware.spark.util.log.Log;

public class CrowdChatRoom extends ChatRoom{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1234567L;
	/**
	 * 
	 */
	private CrowdsMessageManager messageManager;
	
	/**
	 * 
	 */
	private String threadID;
	/**
	 * crowd name ,or crowd JID's node
	 */
	private String name;
	/**
	 * 
	 */
    private final Color TRANSPARENT_COLOR = new Color(0,0,0,0);
    /**
     * 
     */
	private long lastActivity;
	
	private MemberListPane memberListPane;
	
	
	public CrowdChatRoom(String name){
		super();
		this.name = name ;
		
		this.messageManager = CrowdsMessageManager.getInstance(Param.getConnection(), Param.CROWD_SERVICE_NAME);
		
	}
	public void decorate(){
		
		try {
			memberListPane = new MemberListPane(name);
		} catch (Exception e) {			
			e.printStackTrace();
			CrowdRecorder.record(e.getMessage());
		}
		JSplitPane split = this.getSplitPane();
		
		this.getTranscriptWindow().setBorder(new EmptyBorder(0,0,0,0));
		
		split.setRightComponent(memberListPane);
		split.setResizeWeight(1);
		split.setDividerLocation(0.3);
		split.setDividerSize(1);
		
		
		
	}
	
	public void receiveMessage(String message){
		
	}
	@Override
	public Type getChatType() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getLastActivity() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getRoomTitle() {
		// TODO Auto-generated method stub
		return name;
	}

	@Override
	public String getRoomname() {
		// TODO Auto-generated method stub
		return name;
	}

	@Override
	public Icon getTabIcon() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getTabTitle() {
		
		return name;
	}

	@Override
	public boolean isActive() {
		
		return true;
	}

	@Override
	public void leaveChatRoom() {
		
		this.memberListPane.getListModel().removeAllElements();
		
		

		// Remove Packet Listener
		SparkManager.getConnection().removePacketListener(this);

		// Disable Send Field
		getChatInputEditor().showAsDisabled();

		// Do not allow other to try and invite or transfer chat
		disableToolbar();

		getToolBar().setVisible(false);

		// Update Room Notice To Inform Agent that he has left the chat.
		getTranscriptWindow().insertNotificationMessage(
			Res.getString("message.user.left.room", getNickname()),
			ChatManager.NOTIFICATION_COLOR);

		// Leave the Chat.
		

		// Set window as greyed out.
		getTranscriptWindow().showWindowDisabled();

		// Update Notification Label
		getNotificationLabel().setText(
			Res.getString("message.chat.session.ended",
				SparkManager.DATE_SECOND_FORMATTER
					.format(new java.util.Date())));
		
		getNotificationLabel().setIcon(null);
		getNotificationLabel().setEnabled(false);

		getSplitPane().setRightComponent(null);
		getSplitPane().setDividerSize(0);
		
	}
	/**
	 * copy form {@link ChatRoomImpl}
	 */
	@Override
	protected void sendMessage() {
		lastActivity = System.currentTimeMillis();
		Message message = null;
		 String text = getChatInputEditor().getText();
	     try {
	    	
			message = CrowdsManager.getInstance().sendMessage(name, text);
			 
			 getTranscriptWindow().insertMessage(getNickname(), message, ChatManager.TO_COLOR, TRANSPARENT_COLOR);
	         getChatInputEditor().selectAll();

	         getTranscriptWindow().validate();
	         getTranscriptWindow().repaint();
	         getChatInputEditor().clear();
			
	         
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		fireMessageSent(message);
		 /**
         * if add to transcript , all the "from" will be "crowd.127.0.0.1"
         */
//        addToTranscript(message, false);
		
		getChatInputEditor().setCaretPosition(0);
        getChatInputEditor().requestFocusInWindow();
        scrollToBottom();
        
//        MessageEventManager.addNotificationsRequests(message, true, false, false, true);
        
        try {
//            fireOutgoingMessageSending(message);          
        }
        catch (Exception ex) {
            Log.error("Error sending message", ex);
        }
        
	}
	
	@Override
	protected void sendMessage(String text) {
	    
	}
	
	@Override
	public void sendMessage(Message message) {
      
	}
	
	 public void fireOutgoingMessageSending(Message message) {
		 
	 }
	 /**
	  * represents  all the members of a crowd  on the {@link MemberListPane} this 
	  * method will be called when {@link Crowd} received members .Must make
	  * sure that the same member can't be repeated.
	  * @param members
	  */
	public void showMemberList(List<Member> members) {
		// TODO Auto-generated method stub
		
	}
	public MemberListPane getMemberListPane() {
		return memberListPane;
	}
	
	public void closeChatRoom(){
		super.closeChatRoom();
		this.memberListPane.getListModel().removeAllElements();
	}
	
}
